package com.android.jay.lib_audio.mediaplayer.core;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import android.text.TextUtils;
import android.util.Log;

import com.android.jay.lib_audio.app.AudioHelper;
import com.android.jay.lib_audio.mediaplayer.event.AudioFavouriteEvent;
import com.android.jay.lib_audio.mediaplayer.event.AudioLoadEvent;
import com.android.jay.lib_audio.mediaplayer.event.AudioPauseEvent;
import com.android.jay.lib_audio.mediaplayer.event.AudioStartEvent;
import com.android.jay.lib_audio.mediaplayer.model.AudioBean;
import com.android.jay.lib_audio.view.NotificationHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.android.jay.lib_audio.view.NotificationHelper.NOTIFICATION_ID;

/**
 * Created by wzp on 2020/1/30 0030.
 * <p>
 * 描述  ：音乐后台服务并更新notification状态
 */
public class MusicService extends Service implements NotificationHelper.NotificationHelperListener {

    /**
     * 常量区
     **/

    private static String DATA_AUDIOS = "AUDIOS";
    private static String ACTION_START = "ACTION_START";

    /**
     * DATA
     **/
    private ArrayList<AudioBean> mAudioBeans;
    private NotificationReceiver mReceiver;


    /**
     * 外部启动service方法
     **/

    public static void startMusicService(ArrayList<AudioBean> audioBeans) {
        Intent intent = new Intent(AudioHelper.getContext(), MusicService.class);
        intent.setAction(ACTION_START);
        //还需要传一个list
        intent.putExtra(DATA_AUDIOS, audioBeans);
        AudioHelper.getContext().startService(intent);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        registerBrocastReceiver();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mAudioBeans = (ArrayList<AudioBean>) intent.getSerializableExtra(DATA_AUDIOS);

        if (intent.getAction().equals(ACTION_START)) {
            //播放音乐
            playMusic();
            //初始化notification
            NotificationHelper.getInstance().init(this);
        }
        return super.onStartCommand(intent, flags, startId);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unRegisterBrocastReceiver();
    }

    private void unRegisterBrocastReceiver() {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
        }
    }

    private void registerBrocastReceiver() {
        //动态注册广播
        if (mReceiver == null) {
            mReceiver = new NotificationReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(NotificationReceiver.ACTION_STATUS_BAR);
            registerReceiver(mReceiver, filter);

        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private void playMusic() {
        AudioController.getInstance().setQueue(mAudioBeans);
        AudioController.getInstance().play();
    }

    @Override
    public void onNotificationInit() {
        //notificatio与service的绑定让service成为一个前台的服务
        startForeground(NOTIFICATION_ID, NotificationHelper.getInstance().getNotification());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioLoadEvent(AudioLoadEvent event) {
        //更新notification状态
        NotificationHelper.getInstance().showLoadingStatus(event.mAudioBean);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioStartEvent(AudioStartEvent event) {
        NotificationHelper.getInstance().showPlayStatus();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioPauseEvent(AudioPauseEvent event) {
        NotificationHelper.getInstance().showPauseStatus();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAudioFavouriteEvent(AudioFavouriteEvent event) {
        NotificationHelper.getInstance().changeFavouriteStatus(event.isFavourite);
    }



    /**
     * 接受notification发送的广播
     **/
    public static class NotificationReceiver extends BroadcastReceiver {
        public static final String ACTION_STATUS_BAR =
                AudioHelper.getContext().getPackageName() + ".NOTIFICATION_ACTIONS";

        public static final String EXTRA = "extra";
        public static final String EXTRA_PLAY = "play_pause";
        public static final String EXTRA_NEXT = "play_next";
        public static final String EXTRA_PRE = "play_previous";
        public static final String EXTRA_FAV = "play_favourite";


        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("廣播 ----","1111111111111111111111111");


            if (intent == null || TextUtils.isEmpty(intent.getAction())) {
                return;
            }
            String action = intent.getStringExtra(EXTRA);
            switch (action) {
                case EXTRA_PLAY:
                    AudioController.getInstance().playOrPause();
                    break;
                case EXTRA_NEXT:
                    AudioController.getInstance().next();
                    break;
                case EXTRA_PRE:
                    AudioController.getInstance().previous();
                    break;
                case EXTRA_FAV:
                    //TODO 收藏廣播處理
                    Log.e("收藏","收藏..................");

                    AudioController.getInstance().changeFavouriteState();
                    break;
            }


        }
    }
}
